package com.example.activity.utils;

import lombok.Data;

import javax.servlet.http.HttpServletResponse;

@Data
public class Result<T> {
    private static final int SUCCESS = HttpServletResponse.SC_OK;
    private static final int FAIL = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;

    private Integer code;//状态码
    private String message;//返回信息
    private T data;//数据

    /**
     * 成功true,失败false
     */
    private Boolean success;

    private Result(){}

    public Result(Integer code, String message, T data, Boolean success) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.success = success;
    }

    public static<T> Result<T> ok(){
        return new Result<>(SUCCESS, null, null, Boolean.TRUE);
    }

    public static<T> Result<T> ok(T data){
        return new Result<>(SUCCESS, null, data, Boolean.TRUE);
    }

    public static<T> Result<T> ok(String message, T data){
        return new Result<>(SUCCESS, message, data, Boolean.TRUE);
    }

    public static<T> Result<T> fail(String message){
        return new Result<>(FAIL, message, null, Boolean.FALSE);
    }

    public static<T> Result<T> fail(String message, T data){
        return new Result<>(FAIL, message, data, Boolean.FALSE);
    }

}
